//获取应用实例
var app = getApp();
var index = 1;
Page({
  data: {
    list: [],
    hasMore: true,
    // 是否显示loading
    showLoading: false,
    // loading提示语
    loadingMessage: '',
    // 是否显示toast
    showToast: false,
    // 提示消息
    toastMessage: '',

    canSee: false,

    touch_end: 0,
    touch_start: 0,
  },
  editAddress: function (event) {
    let that = this;
    //触摸时间距离页面打开的毫秒数
    var touchTime = that.data.touch_end - that.data.touch_start;
    console.log(touchTime);
    console.log(event)
    var index = event.currentTarget.id;
    var classify = that.data.list[index];
    var classifyGuid = classify.classifyGuid
    console.log(classifyGuid)
    //如果按下时间大于350为长按
    if (touchTime > 350) {
      if (!this.data.canSee){
        return;
      }
      let that = this;
      wx.showActionSheet({
        itemList: ['删除', '修改'],
        itemColor: "#155D3E",
        success: function (res) {
          console.log(res.tapIndex)
          if (res.tapIndex == 0) {
            wx.showModal({
              title: '提示',
              content: '是否删除该电话簿？',
              success: function (res) {
                if (res.confirm) {
                  wx.showNavigationBarLoading();
                  deleteclassifys(classifyGuid, that);
                }
              }
            })
          }else{
            console.log('修改')  
            wx.navigateTo({
              url: '../updataCategory/updataCategory?classifyGuid=' + classifyGuid + "&classifyName=" + classify.classifyName + '&classifyHeadurl=' + classify.classifyHeadurl
            })
          }
        },
        fail: function (res) {
          console.log(res.errMsg)
        }
      })
    } else {
      console.log('wwwww');
      wx.navigateTo({
        url: '../linkManList/linkManList?classifyGuid=' + classifyGuid
      })
    }
  },
  //按下事件开始
  mytouchstart: function (e) {
    let that = this;
    that.setData({
      touch_start: e.timeStamp
    })
    console.log(e.timeStamp + '- touch-start')
  },
  //按下事件结束
  mytouchend: function (e) {
    let that = this;
    that.setData({
      touch_end: e.timeStamp
    })
    console.log(e.timeStamp + '- touch-end')
  },
  // 显示loading提示
  showLoading(loadingMessage) {
    this.setData({ showLoading: true, loadingMessage });
  },

  // 隐藏loading提示
  hideLoading() {
    this.setData({ showLoading: false, loadingMessage: '' });
  },

  // 显示toast消息
  showToast(toastMessage) {
    this.setData({ showToast: true, toastMessage });
  },

  // 隐藏toast消息
  hideToast() {
    this.setData({ showToast: false, toastMessage: '' });
  },
  // onLoad:function(){
  //   wx.clearStorageSync();
  // },
  onShow: function () {
    // wx.clearStorageSync();
    let that = this
    let userInfo = wx.getStorageSync('userInfo')
    console.log(userInfo);
    if (!userInfo) {
      wx.navigateTo({
        url: "../authorize/authorize"
      })
    } else {
      wx.showNavigationBarLoading()
      // app.getCity(function () {
      //   wx.hideNavigationBarLoading()
      //   wx.setNavigationBarTitle({
      //     title: '通讯录 - ' + app.globalData.city
      //   })
      // })
      var self = this;
      // this.showLoading('加载中...');
      getData(self);

      var userguid = wx.getStorageSync('OPEN_ID');
      if (userguid) {
        getUserByUserGuid(this, userguid);
      }
    }
  },
  onPullDownRefresh: function () {
    var that = this
    that.setData({
      list: [],
      hasMore: true,
      showLoading: true,
    })
    index = 1;
    that.showLoading('加载中...');
    wx.showNavigationBarLoading();
    getData(that);
  },
  onReachBottom: function () {
    var that = this
    if (!that.data.showLoading) {
      wx.showNavigationBarLoading();
      getData(that);
    }
  },
  toAddtxl: function (event) {
    wx.navigateTo({
      url: "../addCategory/addCategory"
    })
  }
})
//获取数据
var getData = function (self) {
  wx.request({
    url: app.globalData.ServiceUrl + 'classify/getAllclassify', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        index: index,
        currentPageCount: 7
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      self.hideLoading();
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        if (res.data.UserArea.list.length == 0) {
          self.setData({
            hasMore: false,
          })
        } else {
          if (res.data.UserArea.list.length < 20) {
            self.setData({
              hasMore: false,
            })
          }
          if (index == 1) {
            self.setData({
              list: res.data.UserArea.list
            });
          } else {
            self.setData({
              list: self.data.list.concat(res.data.UserArea.list)
            });
          }
        }
        index++;
      } else {
        wx.showToast({
          title: '获取数据失败！',
          image: '../../images/img_jinggao.png',
          duration: 1000,
        })
      }
    }
  })
}
var deleteclassifys = function (classifyGuid, self) {
  wx.request({
    url: app.globalData.ServiceUrl + 'classify/deleteClassify', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        classifyGuids: classifyGuid,
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      wx.stopPullDownRefresh();
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 1000
        });
        wx.showNavigationBarLoading()
        index = 1;
        getData(self);
      } else {
        wx.showToast({
          title: '删除失败！',
          image: '../../images/img_jinggao.png',
          duration: 1000,
        })
      }
    }
  })
}

var getUserByUserGuid = function (self, userguid) {
  wx.request({
    url: app.globalData.ServiceUrl + 'user/getUserByUserGuid', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        userGuid: userguid,
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        if (res.data.UserArea.isAdmin == 1) {
          self.setData({
            canSee: true
          })
        }
      }
    }
  })
}